package com.itheima.controller;

import com.itheima.config.AliyunOSSProperties;
import com.itheima.pojo.Result;
import com.itheima.util.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
        // 获取文件名称
        String originalFilename = file.getOriginalFilename();  // 1.jpg
//        String name = file.getName();  // file
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));

        String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), file.getBytes(), extName);
        // 转存
//        file.transferTo(new File("E:/images/" + newFileName));
        return Result.success(url);
    }
}
